using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using Common;

namespace PCR.PCRControls
{
	public partial class Step : UserControl, INotifyPropertyChanged, IComponentConnector
	{
		public static readonly DependencyProperty TemperatureProperty = DependencyProperty.Register("Temperature", typeof(double), typeof(Step), new FrameworkPropertyMetadata(51.5, OnTemperatureChanged));

		private bool TemptEditClick;

		public static readonly DependencyProperty PriorTemptProperty = DependencyProperty.Register("PriorTempt", typeof(double), typeof(Step), new FrameworkPropertyMetadata(25.0, OnPriorTemptChanged));

		private string _StepName;

		public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(Step), new FrameworkPropertyMetadata(false, OnIsSelectedChanged));

		private int _StayMinute;

		private int _StaySecond;

		private bool StackStayTimeClick;

		private bool _ScreenTest;

		private bool _Program;

		private bool _Program1;

		private int _WhileNum = 1;

		private int _AddSecond;

		private int _TimeSecond;

		private int _AddTempt;

		private double _TemptChange;

		private double _TemptRate;

		private bool Click;

		public double Temperature
		{
			get
			{
				return (double)GetValue(TemperatureProperty);
			}
			set
			{
				if (value < 4.0)
				{
					value = 4.0;
				}
				if (value > 99.0)
				{
					value = 99.0;
				}
				if (ConfigReader.GetInstance().GetAcquisitionScanLimit() && value < 15.0)
				{
					ScreenTest = false;
				}
				SetValue(TemperatureProperty, value);
			}
		}

		public double PriorTempt
		{
			get
			{
				return (double)GetValue(PriorTemptProperty);
			}
			set
			{
				if ((double)GetValue(PriorTemptProperty) != value)
				{
					SetValue(PriorTemptProperty, value);
					OnPropertyChanged("PriorTempt");
				}
			}
		}

		public string StepName
		{
			get
			{
				return _StepName;
			}
			set
			{
				if (_StepName != value && value != null)
				{
					_StepName = value.Trim();
					OnPropertyChanged("StepName");
				}
			}
		}

		public bool IsSelected
		{
			get
			{
				return (bool)GetValue(IsSelectedProperty);
			}
			set
			{
				SetValue(IsSelectedProperty, value);
			}
		}

		public int StayMinute
		{
			get
			{
				return _StayMinute;
			}
			set
			{
				if (value < 0)
				{
					value = 0;
				}
				if (value > 99)
				{
					value = 99;
				}
				if (value != _StayMinute)
				{
					_StayMinute = value;
					OnPropertyChanged("StayMinute");
				}
			}
		}

		public int StaySecond
		{
			get
			{
				return _StaySecond;
			}
			set
			{
				if (value < 0)
				{
					value = 0;
				}
				if (value > 59)
				{
					value = 59;
				}
				if (value != _StaySecond)
				{
					_StaySecond = value;
					OnPropertyChanged("StaySecond");
				}
			}
		}

		public bool ScreenTest
		{
			get
			{
				return _ScreenTest;
			}
			set
			{
				if (_ScreenTest == value)
				{
					return;
				}
				if (ConfigReader.GetInstance().GetAcquisitionScanLimit() && StayMinute * 60 + StaySecond < 15)
				{
					MessageBox.Show(string.Format(Utility.GetMessage("msg_screen_time_out_of_range"), 14), Utility.GetMessage("Warning"));
					value = false;
				}
				if (value)
				{
					foreach (Step item in SParent.Items)
					{
						if (item != this && item.ScreenTest)
						{
							item.ScreenTest = false;
						}
					}
				}
				_ScreenTest = value;
				OnPropertyChanged("ScreenTest");
			}
		}

		public Section SParent { get; set; }

		public bool Program
		{
			get
			{
				return _Program;
			}
			set
			{
				if (_Program != value)
				{
					_Program = value;
					OnPropertyChanged("Program");
				}
			}
		}

		public bool Program1
		{
			get
			{
				return _Program1;
			}
			set
			{
				if (_Program1 != value)
				{
					_Program1 = value;
					OnPropertyChanged("Program1");
				}
			}
		}

		public int WhileNum
		{
			get
			{
				return _WhileNum;
			}
			set
			{
				if (_WhileNum != value)
				{
					if (value < 1)
					{
						value = 1;
					}
					if (value > SParent.WhileNum)
					{
						value = SParent.WhileNum;
					}
					_WhileNum = value;
					OnPropertyChanged("WhileNum");
				}
			}
		}

		public int AddSecond
		{
			get
			{
				return _AddSecond;
			}
			set
			{
				if (_AddSecond != value)
				{
					_AddSecond = value;
					OnPropertyChanged("AddSecond");
				}
			}
		}

		public int TimeSecond
		{
			get
			{
				return _TimeSecond;
			}
			set
			{
				if (_TimeSecond != value)
				{
					_TimeSecond = value;
					int num = CheckTime();
					if (num > -1)
					{
						_TimeSecond = num;
					}
					OnPropertyChanged("TimeSecond");
				}
			}
		}

		public int AddTempt
		{
			get
			{
				return _AddTempt;
			}
			set
			{
				if (_AddTempt != value)
				{
					_AddTempt = value;
					ReviseTempt();
					OnPropertyChanged("AddTempt");
				}
			}
		}

		public double TemptChange
		{
			get
			{
				return _TemptChange;
			}
			set
			{
				if (_TemptChange != value)
				{
					_TemptChange = value;
					double num = CheckTempt();
					if (num > -1.0)
					{
						_TemptChange = num;
					}
					OnPropertyChanged("TemptChange");
				}
			}
		}

		public double TemptRate
		{
			get
			{
				return _TemptRate;
			}
			set
			{
				if (value < 0.1)
				{
					value = 0.1;
				}
				if (value > 4.5)
				{
					value = 4.5;
				}
				_TemptRate = value;
				OnPropertyChanged("TemptRate");
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;

		public Step()
		{
			InitializeComponent();
			Temperature = 95.0;
			StayMinute = 0;
			StaySecond = 10;
		}

		private static void OnTemperatureChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			Step step = obj as Step;
			step.Temperature = step.Temperature;
			step.cMarginTop.Height = new GridLength(99.0 - step.Temperature, GridUnitType.Star);
			step.cMarginBot.Height = new GridLength(step.Temperature - 4.0, GridUnitType.Star);
		}

		private void TemptEdit_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			TemptEditClick = true;
			e.Handled = true;
		}

		private void TemptEdit_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			if (TemptEditClick)
			{
				TemptEditClick = false;
				cTemperature.Visibility = Visibility.Visible;
				cTemperature1.Visibility = Visibility.Collapsed;
				cTemperature.Focus();
			}
		}

		private void cTemperature_LostFocus(object sender, RoutedEventArgs e)
		{
			cTemperature1.Visibility = Visibility.Visible;
			cTemperature.Visibility = Visibility.Collapsed;
		}

		private static void OnPriorTemptChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
		}

		private static void OnIsSelectedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			Step step = obj as Step;
			if (step.IsSelected)
			{
				step.Background = new SolidColorBrush(Colors.LightPink);
			}
			else
			{
				step.Background = new SolidColorBrush(Colors.Transparent);
			}
		}

		private void StackStayTime_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			StackStayTimeClick = true;
			e.Handled = true;
		}

		private void StackStayTime_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			if (StackStayTimeClick)
			{
				StackStayTimeClick = false;
				cStayTime.Visibility = Visibility.Visible;
				cStayTime.Focus();
				cStayMinute1.Visibility = Visibility.Collapsed;
			}
		}

		private void cStayTime_LostFocus(object sender, RoutedEventArgs e)
		{
			if (Keyboard.FocusedElement != cStayTime.second && Keyboard.FocusedElement != cStayTime.minute && Keyboard.FocusedElement != cStayTime)
			{
				cStayMinute1.Visibility = Visibility.Visible;
				cStayTime.Visibility = Visibility.Collapsed;
			}
			if (_ScreenTest && ConfigReader.GetInstance().GetAcquisitionScanLimit() && StayMinute * 60 + StaySecond < 15)
			{
				StaySecond = 15;
			}
		}

		public void OnPropertyChanged(string propName)
		{
			if (string.IsNullOrEmpty(propName))
			{
				throw new ArgumentException("propName");
			}
			if (this.PropertyChanged != null)
			{
				this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
			}
		}

		public bool EditProgram()
		{
			HighProgram highProgram = new HighProgram(this);
			highProgram.DataContext = this;
			highProgram.Owner = Utility.GetMainWindow();
			return highProgram.ShowDialog() == true;
		}

		public int CheckTime()
		{
			if (AddSecond == 0)
			{
				int num = (5999 - (StayMinute * 60 + StaySecond)) / (SParent.WhileNum - WhileNum + 1);
				if (_TimeSecond > num)
				{
					return num;
				}
			}
			else
			{
				int num2 = (StayMinute * 60 + StaySecond) / (SParent.WhileNum - WhileNum + 1);
				if (_TimeSecond > num2)
				{
					return num2;
				}
			}
			return -1;
		}

		public void ReviseTime()
		{
			int num = CheckTime();
			if (num > -1)
			{
				_TimeSecond = num;
				OnPropertyChanged("TimeSecond");
			}
		}

		public double CheckTempt()
		{
			if (AddTempt == 0)
			{
				double num = (99.0 - Temperature) / (double)(SParent.WhileNum - WhileNum + 1);
				if (_TemptChange > num)
				{
					return num;
				}
			}
			else
			{
				double num2 = (Temperature - 4.0) / (double)(SParent.WhileNum - WhileNum + 1);
				if (_TemptChange > num2)
				{
					return num2;
				}
			}
			return -1.0;
		}

		public void ReviseTempt()
		{
			double num = CheckTempt();
			if (num > -1.0)
			{
				_TemptChange = num;
				OnPropertyChanged("TemptChange");
			}
		}

		private void GridSplitter_DragDelta(object sender, DragDeltaEventArgs e)
		{
			double num = cMarginBot.ActualHeight + cMarginTop.ActualHeight;
			if (num <= 0.0)
			{
				num = 1.0;
			}
			Temperature = Convert.ToInt16(cMarginBot.ActualHeight / num * 95.0) + 4;
			BindingExpression bindingExpression = cTemperature.GetBindingExpression(NumSpan.ValueProperty);
			if (bindingExpression != null)
			{
				bindingExpression.UpdateSource();
			}
		}

		private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			Click = true;
		}

		private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			if (Click)
			{
				Click = false;
				IsSelected = !IsSelected;
			}
		}

		[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
		[DebuggerNonUserCode]
		internal Delegate _CreateDelegate(Type delegateType, string handler)
		{
			return Delegate.CreateDelegate(delegateType, this, handler);
		}
	}
}
